package com.example.city_home_app.UI;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.city_home_app.R;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UIUtils {
    private static Toast toast;
    public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;
    public static final int LENGTH_LONG = Toast.LENGTH_LONG;

    public static final String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    /**
     * 解决小米最新系统Toast消息自带app名的改动
     */
    public static void showToast(Context context, String msg, int LENGTH) {
        if (toast == null) {
            toast = Toast.makeText(context, null, LENGTH);
            toast.setText(msg);
        } else {
            toast.setText(msg);
        }
        toast.show();
    }

    public static boolean isemail(String str) {
        String telRegex = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
        if (TextUtils.isEmpty(str)) {
            return false;
        }else {
            return str.matches(telRegex);
        }
    }

    public static boolean isusername(String str) {
        String telRegex = "^[a-zA-Z0-9_-]{6,16}$";
        if (TextUtils.isEmpty(str)) {
            return false;
        }else {
            return str.matches(telRegex);
        }
    }

    public static boolean isname(String str) {
        String telRegex = "^[\\u4E00-\\u9FA5A-Za-z0-9]{2,16}$";
        if (TextUtils.isEmpty(str)) {
            return false;
        }else {
            return str.matches(telRegex);
        }
    }

    public static SpannableString matcherSearchText(Context context,int color, String text, String keyword) {
        SpannableString ss = new SpannableString(text);

        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);

        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(context.getResources().getColor(color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public static String getFilePathFromUri(Context context, Uri uri) {
        if (uri == null) {
            return "";
        } else {
            String path = null;
            String[] pojo = {MediaStore.Images.Media.DATA};
            Cursor cursor = context.getContentResolver().query(uri, pojo, null, null, null);
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
                cursor.moveToFirst();
                path = cursor.getString(columnIndex);
                cursor.close();
            }
            if (path == null) {
                return "";
            } else {
                return path;
            }
        }
    }

    public static Dialog CreateLoadingDialog(Context context, String msg) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_loading_view);

        TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字
        tipTextView.setText(msg);// 设置加载信息

        Dialog loadingDialog = new Dialog(context, R.style.MyDialogStyle);// 创建自定义样式dialog
        loadingDialog.setCancelable(true); // 是否可以按“返回键”消失
        loadingDialog.setCanceledOnTouchOutside(false); // 点击加载框以外的区域
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局
        /**
         *将显示Dialog的方法封装在这里面
         */
        Window window = loadingDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.PopWindowAnimStyle);
        loadingDialog.show();

        return loadingDialog;

    }

    //移除时间
    public static String removeTime(String time){
        String list[] = time.split(" ");
        return list[0];
    }

}
